package com.lenovo.dil.tiltbaseddemo.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;

public class AppInfo {
	private String appLabel;
	private Drawable appIcon;
	private Intent intent;
	private String pkgName;

	public AppInfo() {
	}

	public String getAppLabel() {
		return appLabel;
	}

	public void setAppLabel(String appName) {
		this.appLabel = appName;
	}

	public Drawable getAppIcon() {
		return appIcon;
	}

	public void setAppIcon(Drawable appIcon) {
		this.appIcon = appIcon;
	}

	public Intent getIntent() {
		return intent;
	}

	public void setIntent(Intent intent) {
		this.intent = intent;
	}

	public String getPkgName() {
		return pkgName;
	}

	public void setPkgName(String pkgName) {
		this.pkgName = pkgName;
	}

	public static List<AppInfo> queryAppInfo(Activity activity) {
		PackageManager pm = activity.getPackageManager(); // 获得PackageManager对象
		Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
		mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);

		List<AppInfo> mlistAppInfo = new ArrayList<AppInfo>();
		// 通过查询，获得所有ResolveInfo对象.
		List<ResolveInfo> resolveInfos = pm.queryIntentActivities(mainIntent,
				0);
		System.out.println(resolveInfos.size());
		// 调用系统排序 ， 根据name排序
		// 该排序很重要，否则只能显示系统应用，而不能列出第三方应用程序
		Collections.sort(resolveInfos,
				new ResolveInfo.DisplayNameComparator(pm));
		if (mlistAppInfo != null) {
			mlistAppInfo.clear();
			for (ResolveInfo reInfo : resolveInfos) {
				String activityName = reInfo.activityInfo.name; // 获得该应用程序的启动Activity的name
				String pkgName = reInfo.activityInfo.packageName; // 获得应用程序的包名
				System.out.println(pkgName);
				if (pkgName.contains("baidusoso")
						|| pkgName.contains("yanqing")
						|| pkgName.contains("lenovo")
						|| pkgName.contains("clouds")
						|| pkgName.contains("android")
						|| pkgName.contains("google")
						|| pkgName.contains("lenovo")
						|| pkgName.contains("samsung")) {
					continue;
				}
				String appLabel = (String) reInfo.loadLabel(pm); // 获得应用程序的Label
				Drawable icon = reInfo.loadIcon(pm); // 获得应用程序图标
				// 为应用程序的启动Activity 准备Intent
				Intent launchIntent = new Intent();
				launchIntent.setComponent(new ComponentName(pkgName,
						activityName));
				// 创建一个AppInfo对象，并赋值
				AppInfo appInfo = new AppInfo();
				appInfo.setAppLabel(appLabel);
				appInfo.setPkgName(pkgName);
				appInfo.setAppIcon(icon);
				appInfo.setIntent(launchIntent);
				mlistAppInfo.add(appInfo); // 添加至列表中
			}
		}
		return mlistAppInfo;
	}

	public static List<AppInfo> queryFilterAppInfo(Activity activity) {
		PackageManager pm = activity.getPackageManager();
		// 查询所有已经安装的应用程序
		List<ApplicationInfo> listAppcations = pm
				.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);
		Collections.sort(listAppcations,
				new ApplicationInfo.DisplayNameComparator(pm));// 排序
		List<AppInfo> appInfos = new ArrayList<AppInfo>(); // 保存过滤查到的AppInfo
		// // 根据条件来过滤
		// switch (filter) {
		// case FILTER_ALL_APP: // 所有应用程序
		// appInfos.clear();
		// for (ApplicationInfo app : listAppcations) {
		// AppInfo appInfo = getAppInfo(pm, app);
		// if (appInfo.getAppLabel().indexOf(".") > 0
		// || appInfo.getPkgName().contains("android")
		// || appInfo.getPkgName().contains("samsung")
		// || appInfo.getPkgName().contains("google")
		// || appInfo.getPkgName().contains("lenovo")
		// || appInfo.getPkgName().contains("system")
		// || appInfo.getPkgName().contains("sec")
		// || appInfo.getAppLabel().contains("三星")
		// || appInfo.getAppLabel().toLowerCase().contains("test")
		// || appInfo.getAppLabel().toLowerCase().contains("phone")) {
		// continue;
		// }
		// appInfos.add(appInfo);
		// }
		// Collections.shuffle(appInfos);
		// return appInfos;
		// case FILTER_SYSTEM_APP: // 系统程序
		// appInfos.clear();
		// for (ApplicationInfo app : listAppcations) {
		// if ((app.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
		// appInfos.add(getAppInfo(app));
		// }
		// }
		// return appInfos;
		// case FILTER_THIRD_APP: // 第三方应用程序
//		appInfos.clear();
		for (ApplicationInfo app : listAppcations) {
			// 非系统程序
			if ((app.flags & ApplicationInfo.FLAG_SYSTEM) <= 0) {
				AppInfo appInfo = getAppInfo(pm, app);
				if (appInfo.getPkgName().contains("baidusoso")
						|| appInfo.getPkgName().contains("yanqing")
						|| appInfo.getPkgName().contains("lenovo")
						|| appInfo.getPkgName().contains("clouds")) {
					continue;
				}
				appInfos.add(appInfo);
			}
			// 本来是系统程序，被用户手动更新后，该系统程序也成为第三方应用程序了
			// else if ((app.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) !=
			// 0) {
			// appInfos.add(getAppInfo(pm, app));
			// }
		}
		// break;
		// case FILTER_SDCARD_APP: // 安装在SDCard的应用程序
		// appInfos.clear();
		// for (ApplicationInfo app : listAppcations) {
		// if ((app.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) != 0) {
		// appInfos.add(getAppInfo(pm,app));
		// }
		// }
		// return appInfos;
		// default:
		// return null;
		// }
		Collections.shuffle(appInfos);
		return appInfos;
	}

	// 构造一个AppInfo对象 ，并赋值
	private static AppInfo getAppInfo(PackageManager pm, ApplicationInfo app) {
		AppInfo appInfo = new AppInfo();
		appInfo.setAppLabel((String) app.loadLabel(pm));
		appInfo.setAppIcon(app.loadIcon(pm));
		appInfo.setPkgName(app.packageName);
		return appInfo;
	}
}
